<?php

class Morenews_Model
{
	/**
	 * Holds instance of database connection
	 */
	private $db;
		
	public function __construct()
	{
		$this->db = new MysqlImproved_Driver;
	}
	
	public function get_article($link_title)
	{		
		//connect to database
		$this->db->connect();
		$link_title = $this->db->escape($link_title);
		$this->get_page_data();
		$article['page_title'] = $this->page_data['title'];
		$article['page_description'] = $this->page_data['description'];
		$article['content'] = $this->get_news_data($link_title);
		$article['description'] = $this->get_news_description($link_title);
		$article['keywords'] = $this->get_news_keywords($link_title);
		return $article;
	}
	
	public function get_news_data($link_title){
		$this->db->prepare(
				"
				SELECT
				`news_title`,
				`news_link`,
				`news_creation_date`,
				`news_content`,
				`news_image`
				FROM
				`news`
				WHERE
				`news_link` = '$link_title'
				LIMIT
				1
				;
				"
				);
		$this->db->query();
		$news_content = "";
		while($news_data = $this->db->fetch('array')){
			$news_data['news_creation_date'] = date("Y-m-d", strtotime($news_data['news_creation_date']));
			$news_content .= "
					<!-- Begin News Item -->
					
					<div id=\"item-info\" class=\"item-info\">
						<h4 id=\"news-h\" class=\"news-h\">".$news_data['news_title']."</h4>
						<span class=\"left date\">".$news_data['news_creation_date']."</span>
							
						<div id=\"ie6-fix\" class=\"left\">
							".$news_data['news_content']."
						</div>

					</div>
					
					<!-- End News Item -->
			";
		}
		return $news_content;
	}
	
	public function get_news_description($link_title){
		
		$this->db->prepare(
				"
				SELECT
				`news_meta_description`
				FROM
				`news`
				WHERE
				`news_link` = '$link_title'
				LIMIT
				1
				;
				"
		);
		$this->db->query();
		while($news_data = $this->db->fetch('array')){
			$news_description = $news_data['news_meta_description'];
		}
		return $news_description;
	}
	
	public function get_news_keywords($link_title){
		$this->db->prepare(
				"
				SELECT
				`news_keywords`
				FROM
				`news`
				WHERE
				`news_link` = '$link_title'
				LIMIT
				1
				;
				"
		);
				$this->db->query();
				while($news_datas = $this->db->fetch('array')){
				$news_keywords = $news_datas['news_keywords'];
		}
		return $news_keywords;
	}
	
	public function get_page_data(){
		//prepare query
		$this->db->prepare
		(
				"
				SELECT
				`page`,
				`title`,
				`description`
				FROM
				`page_description`
				WHERE
				`page` = 'news'
				LIMIT
				1
				;
				"
		);
	
		//execute query
		$this->db->query();
	
		$this->page_data = $this->db->fetch('array');
		return $this->page_data;
	}
	
}

